package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.util.BeanHelper;
import com.heima.model.article.dtos.AuthorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2021-11-24
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {

    /**
     * 保存作者
     * @param dto
     * @return
     */
    @Override
    public Integer saveAuthor(AuthorDto dto) {
//        根据apUserId 查询作者信息
        QueryWrapper<ApAuthor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApAuthor::getUserId,dto.getUserId());
        ApAuthor apAuthor = getOne(queryWrapper);
//        如果存在直接返回id
        if(apAuthor!= null){
            return apAuthor.getId();
        }
//        不存在就保存
        apAuthor = BeanHelper.copyProperties(dto,ApAuthor.class);
        apAuthor.setCreatedTime(new Date());
        save(apAuthor);
        return apAuthor.getId();
    }

    @Override
    public ApAuthor findByWmUserId(Integer wmUserId) {
        QueryWrapper<ApAuthor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApAuthor::getWmUserId,wmUserId);
        ApAuthor apAuthor = getOne(queryWrapper);
        return apAuthor;
    }
}
